package com.study.web.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.study.app.service.IAppLoginService;
import com.study.common.annotation.RepeatSubmit;
import com.study.common.constant.Constants;
import com.study.common.core.domain.R;
import com.study.common.core.domain.model.AppEmailRegisterBody;
import com.study.common.core.domain.model.AppRegisterBody;
import com.study.app.service.IAppRegisterService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录验证
 *
 * @author mjzx
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping
public class RegisterController {

    private final IAppRegisterService iAppRegisterService;
    private final IAppLoginService iAppLoginService;

    /**
     * 账号注册
     *
     * @param appRegisterBody 账号注册信息
     * @return 结果
     */
    @RepeatSubmit()
    @SaIgnore
    @PostMapping("/register")
    public R<Map<String, Object>> register(@Validated @RequestBody AppRegisterBody appRegisterBody) {
        Map<String, Object> ajax = new HashMap<>(1);
        // 生成令牌
        String token = iAppRegisterService.register(appRegisterBody);
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }

    /**
     * 邮箱注册
     *
     * @param appEmailRegisterBody 账号注册信息
     * @return 结果
     */
    @RepeatSubmit()
    @SaIgnore
    @PostMapping("/email/register")
    public R<Map<String, Object>> emailRegister(@Validated @RequestBody AppEmailRegisterBody appEmailRegisterBody) {
        appEmailRegisterBody.setPassword(iAppLoginService.decrypt(appEmailRegisterBody.getPassword()));

        Map<String, Object> ajax = new HashMap<>(1);
        // 生成令牌
        String token = iAppRegisterService.emailRegister(appEmailRegisterBody);
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }

}
